/// 内容类型枚举
enum ContentType {
  image, // 图片内容
  video, // 视频内容
}

/// 内容模型
class ContentModel {
  final String id;
  final String userId;
  final String title; // 标题
  final String content;
  final ContentType type;
  final String? imagePath; // 图片路径
  final String? videoPath; // 视频路径
  final String? coverImagePath; // 视频封面图路径
  final DateTime createdAt;
  final int likeCount;
  final int commentCount;

  ContentModel({
    required this.id,
    required this.userId,
    required this.title,
    required this.content,
    required this.type,
    this.imagePath,
    this.videoPath,
    this.coverImagePath,
    required this.createdAt,
    this.likeCount = 0,
    this.commentCount = 0,
  });

  /// 从 JSON 创建内容模型
  factory ContentModel.fromJson(Map<String, dynamic> json) {
    return ContentModel(
      id: json['id'] as String,
      userId: json['userId'] as String,
      title: json['title'] as String,
      content: json['content'] as String,
      type: ContentType.values.firstWhere(
        (e) => e.toString() == 'ContentType.${json['type']}',
        orElse: () => ContentType.image,
      ),
      imagePath: json['imagePath'] as String?,
      videoPath: json['videoPath'] as String?,
      coverImagePath: json['coverImagePath'] as String?,
      createdAt: DateTime.parse(json['createdAt'] as String),
      likeCount: json['likeCount'] as int? ?? 0,
      commentCount: json['commentCount'] as int? ?? 0,
    );
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'title': title,
      'content': content,
      'type': type.toString().split('.').last,
      'imagePath': imagePath,
      'videoPath': videoPath,
      'coverImagePath': coverImagePath,
      'createdAt': createdAt.toIso8601String(),
      'likeCount': likeCount,
      'commentCount': commentCount,
    };
  }

  /// 复制内容模型并修改部分属性
  ContentModel copyWith({
    String? id,
    String? userId,
    String? title,
    String? content,
    ContentType? type,
    String? imagePath,
    String? videoPath,
    String? coverImagePath,
    DateTime? createdAt,
    int? likeCount,
    int? commentCount,
  }) {
    return ContentModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      title: title ?? this.title,
      content: content ?? this.content,
      type: type ?? this.type,
      imagePath: imagePath ?? this.imagePath,
      videoPath: videoPath ?? this.videoPath,
      coverImagePath: coverImagePath ?? this.coverImagePath,
      createdAt: createdAt ?? this.createdAt,
      likeCount: likeCount ?? this.likeCount,
      commentCount: commentCount ?? this.commentCount,
    );
  }

  @override
  String toString() {
    return 'ContentModel(id: $id, userId: $userId, type: $type)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ContentModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

/// 内容评论模型
class ContentCommentModel {
  final String id;
  final String dynamicId;
  final String userId;
  final String content;
  final DateTime createdAt;

  ContentCommentModel({
    required this.id,
    required this.dynamicId,
    required this.userId,
    required this.content,
    required this.createdAt,
  });

  /// 从 JSON 创建评论模型
  factory ContentCommentModel.fromJson(Map<String, dynamic> json) {
    return ContentCommentModel(
      id: json['id'] as String,
      dynamicId: json['dynamicId'] as String,
      userId: json['userId'] as String,
      content: json['content'] as String,
      createdAt: DateTime.parse(json['createdAt'] as String),
    );
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'dynamicId': dynamicId,
      'userId': userId,
      'content': content,
      'createdAt': createdAt.toIso8601String(),
    };
  }

  /// 复制评论模型并修改部分属性
  ContentCommentModel copyWith({
    String? id,
    String? dynamicId,
    String? userId,
    String? content,
    DateTime? createdAt,
  }) {
    return ContentCommentModel(
      id: id ?? this.id,
      dynamicId: dynamicId ?? this.dynamicId,
      userId: userId ?? this.userId,
      content: content ?? this.content,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  String toString() {
    return 'ContentCommentModel(id: $id, dynamicId: $dynamicId, userId: $userId)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ContentCommentModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

/// 内容点赞模型
class ContentLikeModel {
  final String id;
  final String dynamicId;
  final String userId;
  final DateTime createdAt;

  ContentLikeModel({
    required this.id,
    required this.dynamicId,
    required this.userId,
    required this.createdAt,
  });

  /// 从 JSON 创建点赞模型
  factory ContentLikeModel.fromJson(Map<String, dynamic> json) {
    return ContentLikeModel(
      id: json['id'] as String,
      dynamicId: json['dynamicId'] as String,
      userId: json['userId'] as String,
      createdAt: DateTime.parse(json['createdAt'] as String),
    );
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'dynamicId': dynamicId,
      'userId': userId,
      'createdAt': createdAt.toIso8601String(),
    };
  }

  /// 复制点赞模型并修改部分属性
  ContentLikeModel copyWith({
    String? id,
    String? dynamicId,
    String? userId,
    DateTime? createdAt,
  }) {
    return ContentLikeModel(
      id: id ?? this.id,
      dynamicId: dynamicId ?? this.dynamicId,
      userId: userId ?? this.userId,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  String toString() {
    return 'ContentLikeModel(id: $id, dynamicId: $dynamicId, userId: $userId)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ContentLikeModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
